﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;

namespace Alos.Repository
{
    public class ReExamRepository : BaseRepository<ReExam>, IReExamRepository
    {
        /// <summary>
        /// 根据备课编码获取课堂检测
        /// </summary>
        /// <param name="LessionID">备课编码</param>
        /// <param name="TeacherID">老师ID</param>
        /// <returns></returns>
        public ReExam GetReExamByLessionID(int LessionID, int TeacherID)
        {
            string sql = "SELECT top(1)* FROM ReExam WHERE LessionID=@0 AND TeacherID=@1 order by ExamBatch desc";

            return DBContext.SingleOrDefault<ReExam>(sql, LessionID, TeacherID);
        }        
        /// <summary>
        /// 新增1条课堂检测
        /// </summary>
        /// <param name="LessionID">备课编码</param>
        /// <param name="TeacherID">老师ID</param>
        /// <returns></returns>
        public bool CreatReExam(ReExam ReExam)
        {
            var sql = Sql.Builder.Append(@"insert into ReExam(ID,LessionID,TeacherID,ExamBatch,ExamTime,ExamPiont,SubjectNum,RecordTime ,IsEnd) values(@0,@1,@2,@3,@4,@5,@6,@7,@8) ", ReExam.ID, ReExam.LessionID, ReExam.TeacherID, ReExam.ExamBatch, ReExam.ExamTime, ReExam.ExamPiont, ReExam.SubjectNum, ReExam.RecordTime, ReExam.IsEnd);

            return DBContext.Execute(sql)> 0 ? true : false;

        }
        /// <summary>
        /// 修改上一批次课堂测验
        /// </summary>
        /// <param name="ReExamID">课堂测验ID</param>
        /// <returns>课堂检测</returns>

        public bool UpdateReExamByReExamID(string ReExamID)
        {
            var sql = Sql.Builder.Append(@"UPDATE ReExam SET IsEnd=1 WHERE ID=@0 and IsEnd=0  ", ReExamID);
            return DBContext.Execute(sql) > 0 ? true : false;   

        }
    }
}
